package pass

//（24位）划分为4组6位数据
import "encoding/base64"

const alphabet = "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

var bcEncoding = base64.NewEncoding(alphabet)

func base64Encode(src []byte) []byte {
	n := bcEncoding.EncodedLen(len(src))
	dst := make([]byte, n)
	bcEncoding.Encode(dst, src)
	for dst[n-1] == '=' {
		n--
	}
	return dst[:n]
}

func base64Decode(src []byte) ([]byte, error) {
	numOfEquals := 4 - (len(src) % 4)
	var src_cpy []byte = src
	if numOfEquals > 0 {
		src_cpy = make([]byte, len(src), len(src)+numOfEquals)
		copy(src_cpy, src)
		for i := 0; i < numOfEquals; i++ {
			src_cpy = append(src_cpy, '=')
		}
	}

	dst := make([]byte, bcEncoding.DecodedLen(len(src_cpy)))
	n, err := bcEncoding.Decode(dst, src_cpy)
	if err != nil {
		return nil, err
	}
	return dst[:n], nil
}
